<template>
  <component :is="componentFile" @close="close" @refreshParent="refreshParent" @changeTask="changeTask"
             @setCurProcessData="setCurProcessData"
             :bpmId="bpmId"
             :bpmIdType="bpmIdType"
             :processInstanceId="processInstanceId"
             :processDefId="processDefId"
             :formId="formId"
             :formCode="formCode"
             :fks="fks"
             :parentValue="parentValue"
             :buttonDatas="buttonDatas"
             :getBpmOperateVoUrl="getBpmOperateVoUrl"
             v-on="$listeners" v-bind="$attrs"/>
</template>

<script>

  import error from '@/views/common/exception/404'
  import { getAction } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'Redirect',
    components: {
      error
    },
    props: {
      func: {
        type: String,
        default: null
      },
      id: {
        type: String,
        default: null
      },
      code: {
        type: String,
        default: null
      },
      fks: {
        type: Array,
        default: () => []
      },
      parentValue: {
        type: Object,
        default: () => {}
      },
      ignoreSec: Boolean
    },
    data () {
      return {
        componentFile: null,
        url: null,
        buttonDatas: {},
        bpmId: null,
        bpmIdType: null,
        formCode: null,
        processInstanceId: null,
        processDefId: null,
        formId: null,
        modelType: null,
        getBpmOperateVoUrl: null,
        getBpmOperateVo: YchConstant.PREFIX_BPM + '/bpm-front/displayRest/getBpmOperateVo/v1',
        getBpmOperateVoFromManage: YchConstant.PREFIX_BPM + '/bpm-back/processInstanceRest/getBpmOperateVoFromManage/v1'
      }
    },
    inject: ['addRouter'],
    methods: {
      render () {
        this.componentFile = (resolve) => ({
          component: import(`@/views/` + this.url + '.vue'),
          error: error,
          delay: 200,
          timeout: 10000
        })
      },
      //关闭页面
      close () {
        this.$emit('close')
      },
      //刷新父页面
      refreshParent () {
        this.$emit('refreshParent')
      },
      addFromProcess (processDefId) {
        const that = this
        getAction(this.getBpmOperateVo, { id: processDefId, idType: 'processDefId' }).then((res) => {
          that.init(res)
        }).finally(() => {
        })
      },
      addFromBusiness (formCode) {
        const that = this
        getAction(this.getBpmOperateVo, { id: formCode, idType: 'formCode' }).then((res) => {
          that.init(res)
        }).finally(() => {
        })
      },
      detailFromBusiness (formId, formCode) {
        const that = this
        const url = this.ignoreSec ? this.getBpmOperateVoFromManage : this.getBpmOperateVo;

        getAction(url, { id: formId, idType: 'formId', formCode: formCode }).then((res) => {
          that.init(res)
        }).finally(() => {
        })
      },
      detailFromProcess (processInstanceId) {
        const that = this
        getAction(this.getBpmOperateVo, { id: processInstanceId, idType: 'processInstanceId' }).then((res) => {
          that.init(res)
        }).finally(() => {
        })
      },
      detailFromManage (processInstanceId) {
        const that = this
        getAction(this.getBpmOperateVoFromManage, {
          id: processInstanceId,
          idType: 'processInstanceId'
        }).then((res) => {
          that.getBpmOperateVoUrl = that.getBpmOperateVoFromManage
          that.init(res)
        }).finally(() => {
        })
      },
      detailFromTask (taskId) {
        const that = this
        getAction(this.getBpmOperateVo, { id: taskId, idType: 'taskId' }).then((res) => {
          that.init(res)
        }).finally(() => {
        })
      },
      changeTask (task) {
        let router = null
        if (this.$store.getters.formOpenType === '2') {
          router = this.addRouter({
            path: '/bpm/detailFromTask/' + task.id,
            component: resolve => require(['@/views/bpm/redirect/RedirectTab.vue'], resolve),
            props: {
              id: task.id,
              func: 'detailFromTask',
            },
            meta: {
              title: this.$t('m.bpm.common.bpmDetail'),
            }
          });
          if (router) {
            this.$router.push(router).catch(err => {})
          }
        }
        if (!router) {
          this.detailFromTask(task.id)
        }
      },
      init (res) {
        if (res.success) {
          this.buttonDatas = res.result
          if (this.buttonDatas.open) {
            this.url = this.buttonDatas.pcUrl
            this.bpmId = this.buttonDatas.bpmId
            this.bpmIdType = this.buttonDatas.bpmIdType
            this.formCode = this.buttonDatas.formCode
            this.processInstanceId = this.buttonDatas.processInstanceId
            this.processDefId = this.buttonDatas.processDefId
            this.formId = this.buttonDatas.formId
            this.modelType = this.buttonDatas.modelType
            if (this.buttonDatas.title) {
              this.$emit('setTitle', this.buttonDatas.title)
            }
            this.$emit('setModelType', this.modelType)
            this.render()
          } else {
            this.messageUtil.warning(this.$t('m.bpm.verify.tip_no_permission'))
            this.close();
          }
        } else {
          this.messageUtil.warning(res.message)
        }
      },
      setCurProcessData (buttonDatas) {
        //TODO 扩展
        this.processDefId = buttonDatas.processDefId
        this.processInstanceId = buttonDatas.processInstanceId
        if (buttonDatas.title) {
          this.$emit('setTitle', buttonDatas.title)
        }
      }
    },
    created () {
      this[this.func].apply(this, [this.id, this.code])
    }
  }
</script>
